﻿

; (function ($) {

    $.extend({
       
        
        GetSysTimeNumber: function () {//获取当前系统时间  转换为数字
          
            var myDate = new Date();
            //获取当前年
            var year = myDate.getFullYear();
            //获取当前月
            var month = myDate.getMonth() + 1;
            //获取当前日
            var date = myDate.getDate();
            var h = myDate.getHours();       //获取当前小时数(0-23)
            var m = myDate.getMinutes();     //获取当前分钟数(0-59)
            var s = myDate.getSeconds();
            var now = ""+year +  month  +  date    +  h    +  m  +  s ;
            return now;
        },
        GetSysTime: function () {//获取系统当前时间

            var myDate = new Date();
            //获取当前年
            var year = myDate.getFullYear();
            //获取当前月
            var month = myDate.getMonth() + 1;
            //获取当前日
            var date = myDate.getDate();
            var h = myDate.getHours();       //获取当前小时数(0-23)
            var m = myDate.getMinutes();     //获取当前分钟数(0-59)
            var s = myDate.getSeconds();
            var now = year + '-' +  month + "-" +  date + " " + h + ':' +  m  + ":" + s ;
            return now;
        },

        GetDate: function (date) {  //获取日期
            var dt = new Date(date);
            var y = dt.getFullYear();
            var m = dt.getMonth() + 1;
            var d = dt.getDate();
            return y + "-" + m + "-" + d;
        },
        GetTime: function (date) { //获取时间

            var dt = new Date(date);
            var h = dt.getHours();
            var m = dt.getMinutes();
            var s = dt.getSeconds();
            return h + ":" + m + ":" + s;
        },

        GetSysDateTime: function () {//获取时间  带年月日
            var d = new Date()
            var vYear = d.getFullYear()
            var vMon = d.getMonth() + 1
            var vDay = d.getDate()
            var h = d.getHours();
            var m = d.getMinutes();
            var se = d.getSeconds();
            var s = vYear + "年" + (vMon < 10 ? "0" + vMon : vMon) + "月" + (vDay < 10 ? "0" + vDay : vDay) + "日 " + (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (se < 10 ? "0" + se : se);
            return s;
        },
        //获取 城市  区  联动数据
        GetPCR: function (p,c,r) {
           
            var data = {};
            if (p == 0) {
                data = { PID: 0, CID: 110000, RID: 110100 }
            } else {
                data = { PID: 0, CID: p, RID: c}
            }
            var url = "/DBH/GetPCR";
           
            $.ajax({
                type: "POST",
                url: url,
                dataType: 'json',
                async: false,
                data: data,
                success: function (data) {
                    
                    var phtml = "", chtml = "", rhtml = "";

                    for (var i = 0; i < data.PList.length; i++) {
                        if (data.PList[i].codeID == p)
                            phtml += "<option value=\"" + data.PList[i].codeID + "\" selected>" + data.PList[i].name + "</option>";
                        else
                            phtml += "<option value=\"" + data.PList[i].codeID + "\">" + data.PList[i].name + "</option>";
                    }
                    for (var i = 0; i < data.CList.length; i++) {
                        if (data.CList[i].codeID == c)
                            chtml += "<option value=\"" + data.CList[i].codeID + "\" selected >" + data.CList[i].name + "</option>";
                        else
                            chtml += "<option value=\"" + data.CList[i].codeID + "\"  >" + data.CList[i].name + "</option>";
                    }

                    for (var i = 0; i < data.RList.length; i++) {
                        if (data.RList[i].codeID == r) {
                            rhtml += "<option value=\"" + data.RList[i].codeID + "\" selected >" + data.RList[i].name + "</option>";
                        }
                        rhtml += "<option value=\"" + data.RList[i].codeID + "\">" + data.RList[i].name + "</option>";
                    }
                    $("#ProvinceId").html(phtml);
                    $("#City").html(chtml);
                    $("#Region").html(rhtml);

                    $("#ProvinceId").change(function () {
                        var url = "/DBH/GetPCR";
                        $.ajax({
                            type: "POST",
                            url: url,
                            dataType: 'json',
                            data: { PID: 0, CID: $("#ProvinceId").val(), RID: $("#City").val() },
                            success: function (data) {
                                var phtml = "", chtml = "", rhtml = "";
                                for (var i = 0; i < data.CList.length; i++) {
                                    chtml += "<option value=\"" + data.CList[i].codeID + "\">" + data.CList[i].name + "</option>";
                                }
                                for (var i = 0; i < data.RList.length; i++) {
                                    rhtml += "<option value=\"" + data.RList[i].codeID + "\">" + data.RList[i].name + "</option>";
                                }
                                //$("#City").empty();
                                $("#City").html(chtml);
                                $("#Region").html(rhtml);
                                $("#City").trigger('change');
                            }
                        });

                    });

                    $("#City").change(function () {
                        var url = "/DBH/GetPCR";
                        $.ajax({
                            type: "POST",
                            url: url,
                            dataType: 'json',
                            data: { PID: 0, CID: $("#ProvinceId").val(), RID: $("#City").val() },
                            success: function (data) {
                                var phtml = "", chtml = "", rhtml = "";
                                for (var i = 0; i < data.RList.length; i++) {
                                    rhtml += "<option value=\"" + data.RList[i].codeID + "\">" + data.RList[i].name + "</option>";
                                }
                                $("#Region").html(rhtml);
                            }
                        });

                    });
                }

            });

        },

        //获取url 参数
        GetUrlParamIndex: function (index) {//传参数索引  成1开始
            var urlinfo = window.location.href;//获取url
            var userName = urlinfo.split('?')[1].split('=')[index];//拆分url得到”=”后面的参数
            if (r != null) return userName; return null; 
        },
        //获取url中的参数
        getUrlParamName:function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
         },
        NumShiftChinese: function (num) 
        {
            switch (num) {
                case 0:
                    return "零";
                case 1:
                    return "一";
                case 2:
                    return "二";
                case 3:
                    return "三";
                case 4:
                    return "四";
                case 5:
                    return "五";
                case 6:
                    return "六";
                case 7:
                    return "七";
                case 8:
                    return "八";
                case 9:
                    return "九";
                case 10:
                    return "十";
            }
        },
     
        Base64: function (input) {
            _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
            // public method for encoding
            this.encode = function (input) {
                var output = "";
                if (input == null) return output;
                var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
                var i = 0;
                input = _utf8_encode(input);
                while (i < input.length) {
                    chr1 = input.charCodeAt(i++);
                    chr2 = input.charCodeAt(i++);
                    chr3 = input.charCodeAt(i++);
                    enc1 = chr1 >> 2;
                    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                    enc4 = chr3 & 63;
                    if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
                    } else if (isNaN(chr3)) {
                        enc4 = 64;
                    }
                    output = output +
                    _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                    _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
                }
                return output;
            }
            // public method for decoding
            this.decode = function (input) {
                var output = "";
                if (input == null) return output;
                var chr1, chr2, chr3;
                var enc1, enc2, enc3, enc4;
                var i = 0;
                input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
                while (i < input.length) {
                    enc1 = _keyStr.indexOf(input.charAt(i++));
                    enc2 = _keyStr.indexOf(input.charAt(i++));
                    enc3 = _keyStr.indexOf(input.charAt(i++));
                    enc4 = _keyStr.indexOf(input.charAt(i++));
                    chr1 = (enc1 << 2) | (enc2 >> 4);
                    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                    chr3 = ((enc3 & 3) << 6) | enc4;
                    output = output + String.fromCharCode(chr1);
                    if (enc3 != 64) {
                        output = output + String.fromCharCode(chr2);
                    }
                    if (enc4 != 64) {
                        output = output + String.fromCharCode(chr3);
                    }
                }
                output = _utf8_decode(output);
                return output;
            }
            // private method for UTF-8 encoding
            _utf8_encode = function (string) {
                string = string.replace(/\r\n/g, "\n");
                var utftext = "";
                for (var n = 0; n < string.length; n++) {
                    var c = string.charCodeAt(n);
                    if (c < 128) {
                        utftext += String.fromCharCode(c);
                    } else if ((c > 127) && (c < 2048)) {
                        utftext += String.fromCharCode((c >> 6) | 192);
                        utftext += String.fromCharCode((c & 63) | 128);
                    } else {
                        utftext += String.fromCharCode((c >> 12) | 224);
                        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                        utftext += String.fromCharCode((c & 63) | 128);
                    }
                }
                return utftext;
            }
            // private method for UTF-8 decoding
            _utf8_decode = function (utftext) {
                var string = "";
                var i = 0;
                var c = c1 = c2 = 0;
                while (i < utftext.length) {
                    c = utftext.charCodeAt(i);
                    if (c < 128) {
                        string += String.fromCharCode(c);
                        i++;
                    } else if ((c > 191) && (c < 224)) {
                        c2 = utftext.charCodeAt(i + 1);
                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                        i += 2;
                    } else {
                        c2 = utftext.charCodeAt(i + 1);
                        c3 = utftext.charCodeAt(i + 2);
                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                        i += 3;
                    }
                }
                return string;
            }
        }

    }),
        $.fn.serializeObject = function () {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        },
    $.fn.extend({
        GetEnterprise: function () {

            var e = $(this);
            $.ajax({
                type: 'get',
                url: '/Ashx/CommHanlder.ashx',
                data: { oper: 'GetEnterprise' },
                dataType: 'json',
                success: function (data) {

                    var opStr = ' <option value="-1">--选择公司--</option>';
                    $.each(eval(data), function (idx, obj) {
                        opStr += ' <option value="' + obj.ID + '">' + obj.EnterpriseName + '</option>';
                    })
                    $(e).html("");
                    $(e).append(opStr);
                }
            });
        }
    })

})($my = jQuery);
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

